<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="" prop="userId">
      <el-input v-model="dataForm.userId" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="userName">
      <el-input v-model="dataForm.userName" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="userMobile">
      <el-input v-model="dataForm.userMobile" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="dyMobile">
      <el-input v-model="dataForm.dyMobile" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="授权时间" prop="authTime">
      <el-input v-model="dataForm.authTime" placeholder="授权时间"></el-input>
    </el-form-item>
    <el-form-item label="" prop="accessToken">
      <el-input v-model="dataForm.accessToken" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="refreshToken">
      <el-input v-model="dataForm.refreshToken" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="expiresIn">
      <el-input v-model="dataForm.expiresIn" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="refreshExpiresIn">
      <el-input v-model="dataForm.refreshExpiresIn" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="remark">
      <el-input v-model="dataForm.remark" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="scope">
      <el-input v-model="dataForm.scope" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="openId">
      <el-input v-model="dataForm.openId" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="抖音头像" prop="avatar">
      <el-input v-model="dataForm.avatar" placeholder="抖音头像"></el-input>
    </el-form-item>
    <el-form-item label="名称" prop="name">
      <el-input v-model="dataForm.name" placeholder="名称"></el-input>
    </el-form-item>
    <el-form-item label="" prop="uid">
      <el-input v-model="dataForm.uid" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="master">
      <el-input v-model="dataForm.master" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="secUid">
      <el-input v-model="dataForm.secUid" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="下次刷新refreshtoken时间" prop="nextRenewRefreshTime">
      <el-input v-model="dataForm.nextRenewRefreshTime" placeholder="下次刷新refreshtoken时间"></el-input>
    </el-form-item>
    <el-form-item label="下次刷新accesstoken时间" prop="nextRefreshTime">
      <el-input v-model="dataForm.nextRefreshTime" placeholder="下次刷新accesstoken时间"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          userId: '',
          userName: '',
          userMobile: '',
          dyMobile: '',
          authTime: '',
          accessToken: '',
          refreshToken: '',
          expiresIn: '',
          refreshExpiresIn: '',
          remark: '',
          scope: '',
          openId: '',
          avatar: '',
          name: '',
          uid: '',
          master: '',
          secUid: '',
          nextRenewRefreshTime: '',
          nextRefreshTime: ''
        },
        dataRule: {
          userId: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          userName: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          userMobile: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          dyMobile: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          authTime: [
            { required: true, message: '授权时间不能为空', trigger: 'blur' }
          ],
          accessToken: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          refreshToken: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          expiresIn: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          refreshExpiresIn: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          remark: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          scope: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          openId: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          avatar: [
            { required: true, message: '抖音头像不能为空', trigger: 'blur' }
          ],
          name: [
            { required: true, message: '名称不能为空', trigger: 'blur' }
          ],
          uid: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          master: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          secUid: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          nextRenewRefreshTime: [
            { required: true, message: '下次刷新refreshtoken时间不能为空', trigger: 'blur' }
          ],
          nextRefreshTime: [
            { required: true, message: '下次刷新accesstoken时间不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/account/dyaccountauth/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.userId = data.dyAccountAuth.userId
                this.dataForm.userName = data.dyAccountAuth.userName
                this.dataForm.userMobile = data.dyAccountAuth.userMobile
                this.dataForm.dyMobile = data.dyAccountAuth.dyMobile
                this.dataForm.authTime = data.dyAccountAuth.authTime
                this.dataForm.accessToken = data.dyAccountAuth.accessToken
                this.dataForm.refreshToken = data.dyAccountAuth.refreshToken
                this.dataForm.expiresIn = data.dyAccountAuth.expiresIn
                this.dataForm.refreshExpiresIn = data.dyAccountAuth.refreshExpiresIn
                this.dataForm.remark = data.dyAccountAuth.remark
                this.dataForm.scope = data.dyAccountAuth.scope
                this.dataForm.openId = data.dyAccountAuth.openId
                this.dataForm.avatar = data.dyAccountAuth.avatar
                this.dataForm.name = data.dyAccountAuth.name
                this.dataForm.uid = data.dyAccountAuth.uid
                this.dataForm.master = data.dyAccountAuth.master
                this.dataForm.secUid = data.dyAccountAuth.secUid
                this.dataForm.nextRenewRefreshTime = data.dyAccountAuth.nextRenewRefreshTime
                this.dataForm.nextRefreshTime = data.dyAccountAuth.nextRefreshTime
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/account/dyaccountauth/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'userId': this.dataForm.userId,
                'userName': this.dataForm.userName,
                'userMobile': this.dataForm.userMobile,
                'dyMobile': this.dataForm.dyMobile,
                'authTime': this.dataForm.authTime,
                'accessToken': this.dataForm.accessToken,
                'refreshToken': this.dataForm.refreshToken,
                'expiresIn': this.dataForm.expiresIn,
                'refreshExpiresIn': this.dataForm.refreshExpiresIn,
                'remark': this.dataForm.remark,
                'scope': this.dataForm.scope,
                'openId': this.dataForm.openId,
                'avatar': this.dataForm.avatar,
                'name': this.dataForm.name,
                'uid': this.dataForm.uid,
                'master': this.dataForm.master,
                'secUid': this.dataForm.secUid,
                'nextRenewRefreshTime': this.dataForm.nextRenewRefreshTime,
                'nextRefreshTime': this.dataForm.nextRefreshTime
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
